﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using bestsearch.Resources;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Phone.Tasks;

namespace bestsearch
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // 构造函数
        public DetailsPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
        }

        // 导航页面以将数据上下文设置为列表中的所选项时
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
                string selectedIndex = "";
                if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
                {
                   // int index = int.Parse(selectedIndex);
                    Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        MediaPlayerLauncher mpl = new MediaPlayerLauncher();
                         mpl.Location = MediaLocationType.Install;

            //设置所有控制纽都出现

            mpl.Controls = MediaPlaybackControls.All;

            //设置出现停止按钮以及暂停按钮

            mpl.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop;

            //设置播放的文件

            mpl.Media =new Uri("http://g3.letv.cn/vod/v2/NjkvMjUvMTQvbGV0di11dHMvMTQvdmVyXzAwXzE0LTIxMzU2Njc1LWF2Yy0zMjIwNjEtYWFjLTEyODAwNi02Nzk2OC0zOTM2MTAwLWU4YWIxYmE4MGE4MmE3MjE4MzQxNGVlM2RlZGMwMzU1LTE0MDE2Njk4NzI0OTcubXA0?b=462&mmsid=20926978&tm=1402019122&key=6958fb4f7e1c083d0d82e0bc16485c94&platid=1&splatid=101&playid=0&tss=0&vtype=21&cvid=311354022041&termid=1&format=0&hwtype=un&ostype=Windows7&tag=letv&sign=letv&expect=1&pay=0&rateid=350", UriKind.RelativeOrAbsolute);

            //启动播放

            mpl.Show();

                      
                    }));
/*#region geturl

                    string fromurl = HttpUtility.UrlEncode(selectedIndex.ToString());
                    WebRequest http = WebRequest.Create("http://www.flvcd.com/parse.php?format=&kw=" + selectedIndex) ;
                    // http.Referer = oauth;
                  //  http.Method = "GET";
                  //  http.ContentType = "application/x-www-form-urlencoded";
                   // http.AllowAutoRedirect = true;
                  //  http.KeepAlive = true;
                    //  http.CookieContainer = MyCookieContainer;
                   object rf = new object();  
                    http.BeginGetResponse((r) =>
                    {

                        WebResponse resp = (WebResponse)http.EndGetResponse(r);
                      //  HttpWebResponse httpRequest = (HttpWebResponse) httpRequest.EndGetResponse(r);
                        using ( StreamReader reader = new StreamReader(resp.GetResponseStream()))
                        {
                            string org = reader.ReadToEnd().ToString();
                           // richTextBox1.Text = org;
                           // reader.Close();
                          //  response.Close();
                            resp.Close();
                            
                            string rs = "<a href=\"([^\"]+)\" target=\"_blank\" class=\"link\" ";
                            var match = Regex.Match(org, rs);
                            string rich = match.Groups[1].Value;
                            Deployment.Current.Dispatcher.BeginInvoke(new Action(() => { wb.Navigate(new Uri(rich)); }));

                        }
                    }
                     , null);
#endregion*/
                    /* reader = new StreamReader(response.GetResponseStream());

                   
                    // rich += match.Groups[2].Value;
                    if (rich == "" | rich == null)
                    {
                        Thread.Sleep(5000);
                        HttpWebRequest http2 = WebRequest.Create("http://www.flvcd.com/parse.php?format=&kw=" + fromurl) as HttpWebRequest;
                        // http.Referer = oauth;
                        http2.Method = "GET";
                        http2.ContentType = "application/x-www-form-urlencoded";
                        http2.AllowAutoRedirect = true;
                        http2.KeepAlive = true;
                        //  http.CookieContainer = MyCookieContainer;
                        HttpWebResponse response2 = http2.GetResponse() as HttpWebResponse;


                        StreamReader reader2 = new StreamReader(response2.GetResponseStream());

                        string org2 = reader2.ReadToEnd().ToString();
                        richTextBox1.Text = org2;
                        reader2.Close();
                        response2.Close();
                        r = "<a href=\"([^\"]+)\" target=\"_blank\" class=\"link\" ";
                        var match2 = Regex.Match(org2, r);
                        rich = match2.Groups[1].Value;*/
                }
                }
           
        }

        // 用于生成本地化 ApplicationBar 的示例代码
        //private void BuildLocalizedApplicationBar()
        //{
        //    // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
        //    ApplicationBar = new ApplicationBar();

        //    // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // 使用 AppResources 中的本地化字符串创建新菜单项。
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
